<?php

class Admin_TemplateController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
       $this->_helper->layout()->disableLayout();
       Zend_Loader::loadFile('../application/admin/models/Template.php');
       $template_model = new Template();
       $template_arr = $template_model->listTemplates();
       // print_r($newsletter_arr); EXIT;
       $this->view->template_arr = $template_arr;
     
    }

    public function addAction()
    {
        $this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/admin/forms/Template.php');
        $template_form = new Form_Template();
        $this->view->title = "Add new Template";
        
        $this->view->template_form = $template_form;       
    }
    
    public function doaddtemplateAction()
    {
        $this->_helper->layout()->disableLayout();
    	Zend_Loader::loadFile('../application/admin/forms/Template.php');
		Zend_Loader::loadFile('../application/admin/models/Template.php');
		$form_template = new Form_Template();
		$request = $this->getRequest();
		$template_model = new Template();
		if ($this->getRequest()->isPost()) {
			$form_values = $request->getPost();
			$add_template = $template_model->addTemplate($form_values);
		}
		
		exit;
    }

    public function editAction()
    {
    	$this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/admin/forms/Template.php');
        Zend_Loader::loadFile('../application/admin/models/Template.php');
        
        $template_model = new Template();
        $controller = $this->getRequest()->getControllerName();
		$action = $this->getRequest()->getActionName();
		$template_id = $this->_getParam('template_id', 0);
		$options = array("action"=>$action, "template_id"=>$template_id);        

        $this->view->template_id = $template_id;
        $template_arr = $template_model->getTemplate($template_id);
        $this->view->template_arr = $template_arr;
    }

    public function doupdatetemplateAction()
    {
        $this->_helper->layout()->disableLayout();
   		Zend_Loader::loadFile('../application/admin/models/Template.php');
		Zend_Loader::loadFile('../application/admin/forms/Template.php');
        $newsletter_form = new Form_Template();
        $request = $this->getRequest();
		if ($this->getRequest()->isPost()) {            
			$template_model = new Template();
			$form_values = $request->getPost();
			$update_template = $template_model->updateTemplate($form_values);
		}
		
		exit;
    }

    public function deletetemplateAction()
    {
    	$this->_helper->layout()->disableLayout();
    	Zend_Loader::loadFile('../application/admin/models/Template.php');
        $Template_model = new Template();
        $request = $this->getRequest();
        $template_id = $request->getParam('template_id');
        $Template_model->deleteTemplate($template_id);
        //$this->_helper->redirector('index');      
    }
               
	public function gettemplateAction()
    {

		$template_id=$_POST['template_id'];
		Zend_Loader::loadFile('../application/admin/models/Template.php');
		$Template_model = new Template();
		$template= $Template_model->getTemplatenewsletter($template_id);
		
		$html = $template['template_content_en'] . "|" . $template['template_content_ru'];          	                
		         
		echo $html;
		exit;
	}    
}